package com.erp.controller;


import com.alibaba.excel.EasyExcel;
import com.erp.entity.Goods;
import com.erp.entity.GoodsWarehouse;
import com.erp.entity.Input;
import com.erp.entity.result.Result;
import com.erp.entity.result.ResultStatus;
import com.erp.entity.vo.InputVO;
import com.erp.service.GoodsService;
import com.erp.service.GoodsWarehouseService;
import com.erp.service.InputService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyj
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/input")
public class InputController {

    @Autowired
    private GoodsWarehouseService goodsWarehouseService;

    @Autowired
    private InputService inputService;

    @Autowired
    private GoodsService goodsService;

    
    @GetMapping("/exportAllInput")
    public void exportAllInput(HttpServletResponse response) throws IOException {
        List<InputVO> list = inputService.getAll();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("Input", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), InputVO.class).sheet("表格").doWrite(list);
    }
    
    @GetMapping("/getInputByPage")
    public Result getInputByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        PageHelper.startPage(page,pageSize);
        List<InputVO> list = inputService.getAll();
        PageInfo<InputVO> pageInfo = new PageInfo<>(list);
        return new Result(ResultStatus.SUCCESS, pageInfo);
    }


    @PostMapping("/add")
    public Result insert(@RequestBody Input input) {
        System.out.printf(input.toString());
        System.out.println(input.getGoodsId());
        // 货物入库，需要更新货物仓库中间表，进行存在判断,还需修改货物总数量
        GoodsWarehouse goodsWarehouse = goodsWarehouseService.isExit(input.getGoodsId(), input.getWarehouse());
        if ( goodsWarehouse != null) {
            // 存在 -> 更新
            goodsWarehouse.setNumber(goodsWarehouse.getNumber() + input.getNumber());
            goodsWarehouseService.update(goodsWarehouse);
        } else {
            goodsWarehouse = new GoodsWarehouse();
            goodsWarehouse.setGid(input.getGoodsId());
            goodsWarehouse.setWarehouseName(input.getWarehouse());
            goodsWarehouse.setNumber(input.getNumber());
            goodsWarehouseService.add(goodsWarehouse);
        }
        Goods goods = goodsService.findById(input.getGoodsId());
        goods.setNumber(goods.getNumber() + input.getNumber());
        goodsService.update(goods);
        input.setCreateTime(new Date());
        input.setIsDeleted(false);
        if (inputService.add(input) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    
    @GetMapping("/delete")
    @RequiresRoles("admin")
    public Result delete(@PathParam("id") Integer id) {
        if (inputService.delete(id) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Input input) {

        if (inputService.update(input) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }
}

